package com.service;

import static com.def.GenericDef.PATH_TYPE_REWK;
import static com.def.GenericDef.PROC_IN_PROCESS;
import static com.def.GenericDef.PROC_WAIT;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef._NO;
import static com.def.GenericDef._YES;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IBisPathiDao;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdInDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.mapKeyVal.key.PathIdVerKey;
import com.mapKeyVal.key.StatBoxOpeBnkWoKey;
import com.mapKeyVal.key.WoBoxKey;
import com.model.Bis_pathi;
import com.model.Bis_pathiId;
import com.model.Ret_prd_in;
import com.model.Ret_prd_info;
import com.model.Ret_wo;
import com.tx.Xpinqope.XpinqopeI;
import com.tx.Xpinqope.XpinqopeO;
import com.tx.Xpinqope.XpinqopeO1;
import com.tx.Xpinqope.XpinqopeO2;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpinqopeService")
public class XpinqopeService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpinqopeService.class);
	@Autowired
	private XpinqopeI inTrx;
	@Autowired
	private XpinqopeO outTrx;

	@Autowired
	private IRetBoxDao retBoxDao;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;

	@Autowired
	private IRetWODao retWODao;
	
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	
	private String gszMsgBuf = "";

	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		mqOpe = new MqOpe();

		inTrx = (XpinqopeI) mqOpe.inStr2Obj(strInMsg, inTrx);
		char action_flgAry[] = inTrx.getAction_flg().toCharArray();
		char action_flg = action_flgAry[0];
		switch (action_flg) {
		case 'M':
			summaryOpeInfoFor9C00();
			break;
		default:
			logger.info("mapping no action_flg ,error");
		}
		logger.info(mqOpe.obj2Str(outTrx));
		return mqOpe.obj2Str(outTrx);
	}



	private void summaryOpeInfoFor9C00() {
		String opeId = inTrx.getOpe_id();
		String  prdGrade, boxId, prdStat, lotId, dnNo,bnkFlg,woId,pathId,pathVer;
		
		List<XpinqopeO1> oary1List = new ArrayList<XpinqopeO1>();
		Map<StatBoxOpeBnkWoKey, Integer> rowMap = new HashMap<StatBoxOpeBnkWoKey, Integer>();//在制品
		Map<WoBoxKey,Ret_prd_info> woBoxMap = new HashMap<WoBoxKey,Ret_prd_info>();
		Map<String, Integer> boxMap = new HashMap<String, Integer>();//箱内数量
		Map<String, String> lotMap = new HashMap<String, String>();//批次号
		Map<String, Ret_wo> woMap = new HashMap<String,Ret_wo>();//内部订单Map
		Map<String, Ret_prd_info> prdMap = new HashMap<String,Ret_prd_info>();//内部订单Map
		Map<PathIdVerKey, Bis_pathi> pathMap = new HashMap<PathIdVerKey, Bis_pathi>();//在制品
		String hql = "FROM Ret_prd_info where (prd_stat='WAIT'and nx_ope_no_fk='"+opeId+"')or " +
						"(prd_stat='INPR' and cr_ope_no_fk='"+opeId+"')";
		if(inTrx.getWo_id()!=null){
		   hql=	"FROM Ret_prd_info where (wo_id_fk='"+inTrx.getWo_id()+"' and prd_stat='WAIT'and nx_ope_no_fk='"+opeId+"')or " +
			"(wo_id_fk='"+inTrx.getWo_id()+"' and prd_stat='INPR' and cr_ope_no_fk='"+opeId+"')";
		}
		List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.find(hql);
		
		for (Ret_prd_info retPrdInfo : retPrdInfoList) {
			prdStat = retPrdInfo.getPrd_stat();
			bnkFlg = retPrdInfo.getBnk_flg();
			woId = retPrdInfo.getWo_id_fk();
			//确定箱号:WAIT与已转入箱子的玻璃，显示箱号，INPR未转入箱子的玻璃显示入账箱号。
			boxId = "".equals(retPrdInfo.getBox_id_fk()) ? retPrdInfo.getPv_box_id_fk() : retPrdInfo.getBox_id_fk();
			pathId = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_id_fk() : retPrdInfo.getCr_path_id_fk();
			pathVer = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_ver_fk() : retPrdInfo.getCr_path_ver_fk();
			StatBoxOpeBnkWoKey rowKey = new StatBoxOpeBnkWoKey(prdStat, opeId, boxId,bnkFlg,woId);
			WoBoxKey woBoxKey = new WoBoxKey(boxId,woId);
			PathIdVerKey pathKey = new PathIdVerKey(pathId,pathVer);
			if (rowMap.containsKey(rowKey)) {
				rowMap.put(rowKey, rowMap.get(rowKey) + 1);
			} else {
				rowMap.put(rowKey, 1);
				prdMap.put(boxId, retPrdInfo);
				woBoxMap.put(woBoxKey, retPrdInfo);
			}
			
			//获取批次号，若同一箱内的玻璃批次号有不同，显示为MIX
			lotId = retPrdInfo.getLot_id();
			if(lotId!=null){
				if (lotMap.containsKey(boxId)) {
					if (!lotId.equals(lotMap.get(boxId))) {
						lotMap.put(boxId, "Mix");
					}
				} else {
					lotMap.put(boxId, lotId);
				}
			}
			//获取内部订单信息
			if(woId!=null &&!woId.equals("")){
				if(woMap.containsKey(woId)){
					;
				}else{
					Ret_wo ret_wo = retWODao.get(woId);
					if(ret_wo != null){
						woMap.put(woId, ret_wo);
					}
				}
			}
			//获取途程信息
			if(pathKey!=null){
				if (pathMap.containsKey(pathKey)) {
					;
				} else {
					Bis_pathiId id = new Bis_pathiId();
					id.setPath_id(pathKey.getPath_id());
					id.setPath_ver(pathKey.getPath_ver());
					Bis_pathi bis_pathi = bisPathiDao.get(id);
					if(bis_pathi !=null){
						pathMap.put(pathKey, bis_pathi);
					}
				}
			}
			XpinqopeO1 oary1 = new  XpinqopeO1();
			oary1.setBox_id_fk(boxId);
			oary1.setPrd_seq_id(retPrdInfo.getPrd_seq_id());
			oary1.setFab_sn(retPrdInfo.getFab_sn());
			oary1.setPrd_stat(retPrdInfo.getPrd_stat());
			oary1.setPrd_grade(retPrdInfo.getPrd_grade());
			oary1.setWo_id(woId);
			oary1.setLot_id(lotId);
			oary1.setEvt_usr(retPrdInfo.getEvt_usr());
			oary1.setEvt_timestamp(retPrdInfo.getEvt_timestamp());
			if(retPrdInfo.getPrd_stat().equals(PROC_IN_PROCESS)){
				oary1.setLogof_timestamp(retPrdInfo.getLogof_timestamp());
				oary1.setLogon_timestamp(retPrdInfo.getLogon_timestamp());
				//有上站出账时间和本站释放时间(Move in)
			}else if(retPrdInfo.getPrd_stat().equals(PROC_WAIT)){
				//只有上站出账时间
				oary1.setLogof_timestamp(retPrdInfo.getLogof_timestamp());
			}
			Bis_pathi bis_pathi = pathMap.get(pathKey);
			if(bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)){
				oary1.setRewk_flag(_YES);
			}else{
				oary1.setRewk_flag(_NO);
			}
			oary1List.add(oary1);
		}
		Iterator<Entry<StatBoxOpeBnkWoKey, Integer>> iter = rowMap.entrySet().iterator();
		List<XpinqopeO2> oary2List = new ArrayList<XpinqopeO2>();
		
		StatBoxOpeBnkWoKey rowKey = null;
		while(iter.hasNext()){
			Entry<StatBoxOpeBnkWoKey, Integer> entry = iter.next();
			rowKey = entry.getKey();
			prdStat = rowKey.getPrd_stat();
			boxId = rowKey.getBox_id(); 
			bnkFlg= rowKey.getBnk_flg();
			opeId = rowKey.getOpe_id();
			woId = rowKey.getWo_id();
			Ret_wo retWo = woMap.get(woId);
			
			XpinqopeO2 oary2 = new  XpinqopeO2();
			oary2.setWo_id(woId);
			if(retWo !=null){
				oary2.setCus_id_fk(retWo.getCus_id_fk());
				oary2.setSo_id(retWo.getSo_id());
			}
			oary2.setBox_id(boxId);
			oary2.setLot_id(lotMap.get(boxId));
			oary2.setBox_id(boxId);
			oary2.setBox_stat(prdStat);
			oary2.setPrd_qty(entry.getValue().toString());
			Ret_prd_info ret_prd_info = prdMap.get(boxId);
			WoBoxKey woBoxKey1 = new WoBoxKey(boxId,woId);
			Ret_prd_info ret_prd_info1 = woBoxMap.get(woBoxKey1);
			if(ret_prd_info1.getPrd_stat().equals(PROC_WAIT)){
				PathIdVerKey pathKey = new PathIdVerKey(ret_prd_info1.getNx_path_id_fk(),ret_prd_info1.getNx_path_ver_fk());
				oary2.setLogof_timestamp(ret_prd_info1.getLogof_timestamp());
				oary2.setLogof_usr(ret_prd_info1.getLogof_usr());
				Bis_pathi bis_pathi = pathMap.get(pathKey);
				if(bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)){
					oary2.setRewk_flag(_YES);
				}else{
					oary2.setRewk_flag(_NO);
				}
			}else{
				PathIdVerKey pathKey = new PathIdVerKey(ret_prd_info1.getCr_path_id_fk(),ret_prd_info1.getCr_path_ver_fk());
				Bis_pathi bis_pathi = pathMap.get(pathKey);
				if(bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)){
					oary2.setRewk_flag(_YES);
				}else{
					oary2.setRewk_flag(_NO);
				}
				oary2.setLogof_timestamp(ret_prd_info1.getLogof_timestamp());
				oary2.setLogof_usr(ret_prd_info1.getLogof_usr());
				oary2.setLogon_timestamp(ret_prd_info1.getLogon_timestamp());
				oary2.setLogon_usr(ret_prd_info1.getLogon_usr());
			}
			oary2List.add(oary2);
		}
		if(opeId.equals("JB-01")){
			//返回未发料和已发料的箱子信息
			StringBuffer retPrdInSql =new StringBuffer("FROM Ret_prd_in where  1=1");
			retPrdInSql.append(" and ppbox_stat <>'EMPT'");//in ('WAIT','WFRL')");
			if (!Util.isSpaceCheck(inTrx.getWo_id())) {
				retPrdInSql.append(" and wo_id_fk = '"+inTrx.getWo_id()+"' ");
				System.out.println(inTrx.getWo_id());
			}
			List<Ret_prd_in> retPrdInList = retPrdInDao.list(retPrdInSql.toString());
			for(Ret_prd_in retPrdIn : retPrdInList){
				XpinqopeO2 oary2 = new XpinqopeO2();
				oary2.setWo_id(retPrdIn.getWo_id_fk());
				Ret_wo retWo = woMap.get(retPrdIn.getWo_id_fk());
				if(retWo!=null){
					oary2.setSo_id(retWo.getSo_id());
				}else{
					Ret_wo ret_wo = retWODao.get(retPrdIn.getWo_id_fk());
					if(ret_wo != null){
						woMap.put(retPrdIn.getWo_id_fk(), ret_wo);
						oary2.setSo_id(ret_wo.getSo_id());
					}
				}
				oary2.setBox_id(retPrdIn.getId().getMtrl_box_id());
				oary2.setCus_id_fk(retPrdIn.getId().getCus_id());
				oary2.setLot_id(retPrdIn.getMtrl_lot_id());
				oary2.setRewk_flag(_NO);
				oary2.setPrd_qty(retPrdIn.getPrd_act_qty().toString());
				oary2.setBox_stat("WFRL".equals(retPrdIn.getPpbox_stat())?"已发料":"未发料");
				oary2.setLogon_usr(retPrdIn.getRelate_usr());
				oary2List.add(oary2);
			}
		}
		outTrx.setOary1(oary1List);
		outTrx.setOary1_cnt(oary1List.size());
		outTrx.setOary2(oary2List);
		outTrx.setOary2_cnt(oary2List.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	private Integer fmtInt(Integer val){
		return val == null ? 0 : val;
	}
}
